package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.UIManage;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *跨服战报名 
	 * @author yanghr
	 * 
	 */
	public class StrideAttendLogic
	{
		
		private const MAIN_TEXT:String="";
		private static var _self:StrideAttendLogic;
		public static function getInstance():StrideAttendLogic
		{
			if(_self==null)
			{
				_self=new StrideAttendLogic();
			}
			return _self;
		}
		private var _ui:StrideAttendUI;
		public function get ui():StrideAttendUI 
		{	
			if(_ui==null)
			{
				_ui=new StrideAttendUI();
				UIManage.getInstance().initUI(_ui,"StrideAttendUI");
			}
			return _ui;
		}
			
		public function StrideAttendLogic()
		{
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		
		
		private var _init:Boolean=false;
		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_OPEN_SF_SIGNUP_UI),openUI)
		}
		
		private function set isAlreadyAttend(value:Boolean):void 
		{
			_ui.attendInBtn.visible	=	!value;
			_ui.btnLab.visible	=	value;
		}		
		private function openUI(e:CustomMsgEvent):void
		{
			isAlreadyAttend=e.pList[0];
			showByType(StrideMainLogic.SKY_LOCAL);
			if(!_ui.created)
				_ui.showOrHide();
		}
		private var _type:int;
		/**
		 *类型 
		 * @param type
		 * 
		 */		
		public function showByType(type:int):void
		{
			_type=type;
			_ui.mainText.htmlText=getMainStringByType();
			
			_ui.rewardLab.htmlStr=_tip.getColorHtml(StaticIni.getSwfLangStr("StrideRule_2"),"link");
			_ui.btnLab.htmlText=StaticIni.getSwfLangStr("StrideRule_3");
				
		}
		/**
		 *检查是不是本服战斗 
		 * @return 
		 * 
		 */		
		private function checkTypeSelf():Boolean
		{
			return (_type==StrideMainLogic.SKY_LOCAL||_type==StrideMainLogic.GROUND_LOCAL)
		}
		private function getMainStringByType():String
		{
			if(checkTypeSelf())
				return StaticIni.getSwfLangStr("StrideRule_1");//本服
			return StaticIni.getSwfLangStr("StrideRule_4");//跨服
		}
		public function	BeforeShow():void
		{
			if(!_init)
			{
//				_ui.mainText.htmlText=StaticIni.getSwfLangStr(MAIN_TEXT);
				_ui.bgClip.source=ResPath.PNG+"strideAttend.png";
			}
			addAllEventListener()
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
		}
		public function addAllEventListener():void
		{
			_ui.attendInBtn.addEventListener(MouseEvent.CLICK,attendHandle);
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.rewardLab.addEventListener(MouseEvent.CLICK,linkHandle);
			_ui.rewardLab.addEventListener(MouseEvent.ROLL_OVER,linkHandle);
			_ui.rewardLab.addEventListener(MouseEvent.ROLL_OUT,_tip.hide);
		}
//		private function rollOut(e:Event):void
//		{
//			_tip.hide()
//		}
		private function linkHandle(e:Event):void
		{
			if(checkTypeSelf())
			{
				_tip.showMain(StaticIni.getSwfLangStr("StrideRule_Tips_1"));
			}
			else
			{
				_tip.showMain(StaticIni.getSwfLangStr("StrideRule_Tips_2"));
			}
		}
		private function getTipsStringByType():String
		{
			if(checkTypeSelf())
				return StaticIni.getSwfLangStr("StrideRule_Tips_1");
			return StaticIni.getSwfLangStr("StrideRule_Tips_2");
		}
		/**
		 *报名 
		 * @param e
		 * 
		 */		
		private function attendHandle(e:Event):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SIGN_UP_SERVER_FIGHT);
		}
		public function removeAllEventListener():void
		{
			_ui.attendInBtn.removeEventListener(MouseEvent.CLICK,attendHandle)
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
	}
}